package com.guigu.www.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.www.mapper.MenuRoleMapper;
import com.guigu.www.mapper.MenusMapper;
import com.guigu.www.mapper.RolesMapper;
import com.guigu.www.pojo.MenuRole;
import com.guigu.www.pojo.Menus;
import com.guigu.www.pojo.Roles;
import com.guigu.www.service.MenuRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuRolesServiceImpl extends ServiceImpl<MenuRoleMapper, Roles> implements MenuRolesService {
    @Autowired
    MenuRoleMapper menuRoleMapper;
    @Autowired
    MenusMapper menusMapper;
    @Autowired
    RolesMapper rolesMapper;
    @Override
    public boolean addmenuroles(Roles roles) {
        menuRoleMapper.deleteMenuRoles(roles.getId());
        for (Menus menuInfo : roles.getMenus()) {
            MenuRole roleMenu=new MenuRole();
            roleMenu.setRoleid(roles.getId());
            roleMenu.setMenuid(menuInfo.getId());
            menuRoleMapper.addMenuRoles(roleMenu);
        }
        return true;
    }

    @Override
    public List<Menus> querybyroleidmenu(int roleid) {
        //根据角色id查询拥有菜单list
        List<Menus> thismenu= menusMapper.queryByMenusFuIds(roleid);
        //查询当前一级所有菜单
        List<Menus> onemenu=menusMapper.queryByMenusFuIds(0);
        for (Menus sysMenus : onemenu) {
            List<Menus> twomenu=menusMapper.queryByMenusFuIds(sysMenus.getId());
            for (Menus menus : twomenu) {
                List<Menus> threemenu=menusMapper.queryByMenusFuIds(menus.getId());
                if(threemenu.size()==0){  //三级没有  设置二级在状态

                    if (thismenu.contains(menus.getId())) {
                        menus.setCheck(true);
                    }

                }else{
                    for (Menus threemenu1 : threemenu) {
                        List<Menus> fourmenu=menusMapper.queryByMenusFuIds(threemenu1.getId());
                        if (fourmenu.size() == 0) {
                            if (thismenu.contains(threemenu1.getId())) {
                                threemenu1.setCheck(true);
                            }
                        } else {
                            for (Menus fourmenu1 : fourmenu) {
                                if(thismenu.contains(fourmenu1.getId())){
                                    fourmenu1.setCheck(true);
                                }
                            }
                        }
                        threemenu1.setMenus(fourmenu);
                    }
                }
                menus.setMenus(threemenu);
            }
            sysMenus.setMenus(twomenu);
        }
        return onemenu;
    }
}
